package com.cskaoyan.controller;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.validation.ValidationUser;
import com.cskaoyan.exception.WdValidationException;
import com.cskaoyan.service.UserService;
import com.cskaoyan.util.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author stone
 * @date 2022/10/11 21:22
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    UserService userService;

    // 需求，要做请求参数的校验 → username的长度至少为6位，mobile的长度为11位
    // page=1&limit=20&username=user22&mobile=17801139968&sort=add_time&order=desc
    /*@RequestMapping("list")
    //public BaseRespVo list(Integer page,Integer limit,String username,String mobile,String sort,String order) {
    //public BaseRespVo list(BasePageInfo info, String username, String mobile) {
    public BaseRespVo list(BasePageInfo info, @Valid ValidationUser user, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();
            Object rejectedValue = fieldError.getRejectedValue();
            String defaultMessage = fieldError.getDefaultMessage();
            StringBuffer sb = new StringBuffer();
            String msg = sb.append("请求参数")
                    .append(field)
                    .append("传入的值")
                    .append(rejectedValue)
                    .append("有问题 -- > ")
                    .append(defaultMessage).toString();
            return BaseRespVo.invalidParameter(msg);
        }

        *//*if (username != null && username.length() < 6) {
            return BaseRespVo.invalidData();
        }
        if (password != null && password.length() < 6) {
            return BaseRespVo.invalidData();
        }
        if (mobile != null && mobile.length() != 11) {
            return BaseRespVo.invalidData();
        }*//*

        CommonData<MarketUser> data = userService.list(info, user.getUsername(), user.getMobile());
        return BaseRespVo.ok(data);
    }*/

    /*@RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, @Valid ValidationUser user, BindingResult bindingResult) {
        BaseRespVo msg = ValidationUtil.getBaseRespVo(bindingResult);
        if (msg != null) return msg;

        CommonData<MarketUser> data = userService.list(info, user.getUsername(), user.getMobile());
        return BaseRespVo.ok(data);
    }*/
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo info, @Valid ValidationUser user, BindingResult bindingResult) throws WdValidationException {
        ValidationUtil.valid(bindingResult);

        CommonData<MarketUser> data = userService.list(info, user.getUsername(), user.getMobile());
        return BaseRespVo.ok(data);
    }


}
